﻿var $__mask;
var $__maskDialog;
var $__alertDialog;
var $__selectDialog;
var $__editDialog;
$(function event() {
    $__mask = $("<div style='background-color: Gray;display: none;z-index: 3;position: absolute;left: 0px;top: 0px;filter: Alpha(Opacity=30);-moz-opacity: 0.4;opacity: 0.4;'></div>").appendTo("body");
    var $img = $("<img src='/Images/loading.gif' height='32' width='32'>");
    var $text = $("<div style='text-align:center;font-size:9pt;'>请稍候...</div>");
    $__maskDialog = $("<div id='dialog' style='text-align:center;display: none;z-index: 5;position: absolute;'></div>");
    $__mask.appendTo("body");
    $img.appendTo("<div>").appendTo($__maskDialog);
    $text.appendTo($__maskDialog);
    $__maskDialog.appendTo("body");
    $__alertDialog = $("<div style='font-size:9pt;' title='提示'></div>").appendTo("body");
    $__alertDialog.dialog({
        autoOpen: false,
        width: 600,
        modal: true,
        buttons: {
            "确认": function() {
                $(this).dialog("close");
            }
        }
    });
    $__selectDialog = $("<div style='font-size:9pt;' title='选择'></div>").appendTo("body");
    $__selectDialog.dialog({
        autoOpen: false,
        width: 600,
        modal: true
    });
    $__editDialog = $("<div style='font-size:9pt;'></div>").appendTo("body");
    $__editDialog.dialog({
        autoOpen: false,
        width: 600,
        modal: true
    });
});
GetUrlArg = function(param) {
    var r = new RegExp("\\?(?:.+&)?" + param + "=(.*?)(?:&.*)?$");
    var m = window.location.toString().match(r);
    return m ? decodeURIComponent(m[1]) : "";
}
FormEdit = function(url, actionType, jsonObj, width, height, callback) {
    if (!url || !actionType)
        return;
    if (width)
        $__editDialog.dialog('option', 'width', width);
    if (height)
        $__editDialog.dialog('option', 'height', height);
    var name = "";
    switch (actionType.toLowerCase()) {
        case "datainsert":
            name = "新增";
            break;
        case "dataupdate":
            name = "修改";
            break;
        case "datadelete":
            name = "删除";
            break;
    }
    if (!jsonObj)
        jsonObj = {};
    jsonObj["ActionType"] = actionType;
    $__editDialog.load(url, jsonObj);
    $__editDialog.dialog("option", "title", name);
    $__editDialog.dialog("option", "buttons", {
        "确定": function() {
            callback(__ReturnOk());
            $__editDialog.dialog("close");
        },
        "取消": function() {
            callback(null);
            $__editDialog.dialog("close");
        }
    });
    $__editDialog.dialog('open');
}
ShowModalDialog = function(url, $idsField, $namesField, width, height) {
    if (!url)
        return;
    if (width)
        $__selectDialog.dialog('option', 'width', width);
    if (height)
        $__selectDialog.dialog('option', 'height', height);
    var ids = $idsField.val();
    var names = $namesField.val();
    var jsonobj = { "Ids": ids, "Names": names };
    $__selectDialog.load(url, jsonobj);
    $__selectDialog.dialog("option", "buttons", {
        "确定": function() {
            var jsonobj = __ReturnChecked();
            if (jsonobj == null)
                return;
            $idsField.val(jsonobj["Ids"]);
            $namesField.val(jsonobj["Names"]);
            $__selectDialog.dialog("close");
        },
        "清空": function() {
            $idsField.val("");
            $namesField.val("");
            $__selectDialog.dialog("close");
        },
        "取消": function() {
            $__selectDialog.dialog("close");
        }
    });
    $__selectDialog.dialog('open');
}
Alert = function(message, callBack) {
    $__alertDialog.html(message);
    $__alertDialog.one("dialogclose", callBack);
    $__alertDialog.dialog('open');
    return false;
}
ShowError = function(message, source, stackTrace) {
    Alert(String.format("出错啦！<br><br>Message:{0}<br><br>Source:{1}<br><br>StackTrace:{2}", message, source, stackTrace));
}
ShowMask = function() {
    var bH = $(window).height();
    var bW = $(window).width();
    var objWH = GetObjWh($__maskDialog);
    $__mask.css({ width: bW, height: bH, display: "block" });
    var tbT = objWH.split("|")[0] + "px";
    var tbL = objWH.split("|")[1] + "px";
    $__maskDialog.css({ top: tbT, left: tbL, display: "block" });

    $(window).scroll(function() { ResetMask() });
    $(window).resize(function() { ResetMask() });
}

GetObjWh = function(jqueryObj) {
    var st = document.documentElement.scrollTop; //滚动条距顶部的距离
    var sl = document.documentElement.scrollLeft; //滚动条距左边的距离
    var ch = document.documentElement.clientHeight; //屏幕的高度
    var cw = document.documentElement.clientWidth; //屏幕的宽度
    var objH = jqueryObj.height(); //浮动对象的高度
    var objW = jqueryObj.width(); //浮动对象的宽度
    var objT = Number(st) + (Number(ch) - Number(objH)) / 2;
    var objL = Number(sl) + (Number(cw) - Number(objW)) / 2;
    return objT + "|" + objL;
}

ResetMask = function() {
    var fullbg = $__mask.css("display");
    if (fullbg == "block") {
        var bH2 = $(window).height();
        var bW2 = $(window).width();
        $__mask.css({ width: bW2, height: bH2 });
        var objU = GetObjWh($__mask);
        var maskT = objU.split("|")[0] + "px";
        var maskL = objU.split("|")[1] + "px";
        $__mask.css({ top: maskT, left: maskL });
        var objV = GetObjWh($__maskDialog);
        var tbT = objV.split("|")[0] + "px";
        var tbL = objV.split("|")[1] + "px";
        $__maskDialog.css({ top: tbT, left: tbL });
    }
}

HideMask = function() {
    $__mask.css("display", "none");
    $__maskDialog.css("display", "none");
}

String.format = function() {
    if (arguments.length == 0)
        return null;

    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}

function HtmlEncode(str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&/g, "&amp;");
    s = s.replace(/</g, "&lt;");
    s = s.replace(/>/g, "&gt;");
    s = s.replace(/ /g, "&nbsp;");
    s = s.replace(/\'/g, "&#39;");
    s = s.replace(/\"/g, "&quot;");
    s = s.replace(/\n/g, "<br>");
    return s;
}

function HtmlDecode(str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    s = s.replace(/<br>/g, "\n");
    return s;
} 
   


